package basic;

public class DoubleXYZ implements Comparable<DoubleXYZ> {
	private double x = 0;
	private double y = 0;
	private double z = 0;
	private int axis;

	public DoubleXYZ(double x, double y, double z)
	{
		this.x=x;
		this.y=y;
		this.z=z;
	}

	public double getX() {
		return this.x;
	}

	public double getY() {
		return this.y;
	}

	public double getZ() {
		return this.z;
	}

	public void setX(double x) {
		this.x = x;
	}

	public void setY(double y) {
		this.y = y;
	}

	public void setZ(double z) {
		this.z = z;
	}

	
	public DoubleXYZ setSum(DoubleXYZ p)
	{
		double x=this.getX()+p.getX();
		double y=this.getY()+p.getY();
		double z=this.getZ()+p.getZ();
		DoubleXYZ sum= new DoubleXYZ(x,y,z);
		return sum;
	}

	public DoubleXYZ setProduct(double d)
	{
		return new DoubleXYZ(this.x*d,this.y*d,this.z*d);
	}

	public DoubleXYZ setMinusPoint(DoubleXYZ p)
	{		
		return  new DoubleXYZ(this.getX()-p.getX(),this.getY()-p.getY(),this.getZ()-p.getZ());	
	}

	public double[] getVector()
	{
		double[] temp = new double[3];
		temp[0]=this.getX();
		temp[1]=this.getY();
		temp[2]=this.getZ();
		return temp;
	}

	public double[] setMinus(DoubleXYZ p)
	{
		double[] minus= new double[3];
		minus[0]=this.getX()-p.getX();
		minus[1]=this.getY()-p.getY();
		minus[2]=this.getZ()-p.getZ();
		return minus;
	}

	public double getAxis(int axis)
	{
		if(axis==0) return this.x;
		else if(axis==1) return this.y;
		else return this.z;
	}

	public void setDivide(double s)
	{
		this.x/=s;
		this.y/=s;
		this.z/=s;
	}

	public double dot(double[] norm)
	{
		return this.x*norm[0] + this.y*norm[1] + this.z*norm[2];
	}

	public void setInf(DoubleXYZ point)
	{
		this.x=set_min(this.x,point.x);
		this.y=set_min(this.y,point.y);
		this.z=set_min(this.z,point.z);
	}

	public void setSup(DoubleXYZ point)
	{
		this.x=set_max(this.x,point.x);
		this.y=set_max(this.y,point.y);
		this.z=set_max(this.z,point.z);
	}
	
	public static double set_min(double x,double y)
	{
		if(x>y) return y;
		return x;
	}


	public static double set_max(double x,double y)
	{
		if(x<y) return y;
		return x;
	}


	public double getElement(int index)
	{
		if(index==0) return this.x;
		else if(index==1) return this.y;
		else if(index==2) return this.z;
		else return 0;
	}

	public String toString() {
		return "[" + x + ", " + y + ", " + z + "]";
	}

	/**
	 * metodo compare per il confronto tra i due 
	 * array di punti che vengono creati durante il
	 * controllo dell'intersezione nella gerarchia Aabb
	 */
	public int compareTo(DoubleXYZ f) {
		final int BEFORE = -1;
		final int EQUAL = 0;
		final int AFTER = 1;
		if(this.getElement(this.axis)<f.getElement(f.axis)) return BEFORE;
		else if(this.getElement(this.axis)>f.getElement(f.axis)) return AFTER;
		else return EQUAL;
	}

	public void setAxis(int axis)
	{
		this.axis=axis;
	}
}
